<?php

class ProcedimentosController extends Site_Controller_Action
{
    
    public $limitRegistros = 9;
    
    public function init()
    {
        /* Initialize action controller here */
    }

    public function indexAction()
    {
        $this->view->headScript()->appendFile($this->_urlbase.'/public/media/js/view/procedimento.js?1=1');
        $query = 'SELECT SQL_CACHE PRC_ID,PRC_NOM FROM PROCEDIMENTO WHERE PRC_STA = 1 ORDER BY PRC_ORD ASC LIMIT '.$this->limitRegistros;
        $result = $this->db->fetchAll($query);
        $this->view->result = $result;
        
        $query = 'SELECT SQL_CACHE count(*) as TOTAL  FROM PROCEDIMENTO WHERE PRC_STA = 1';
        $this->view->total = $this->db->fetchRow($query);
        
        
        $this->view->limitRegistros = $this->limitRegistros;
        
    }
    
    public function detalheAction()
    {
        $this->view->headLink()->prependStylesheet($this->_urlbase.'/public/media/js/jquery-ui-1.10.2.custom/css/custom-theme/jquery-ui-1.10.2.custom.css'); 
        $this->view->headScript()->appendFile($this->_urlbase.'/public/media/js/jquery-ui-1.10.2.custom/js/jquery-ui-1.10.2.custom.min.js');
        
        $this->view->headScript()->appendFile($this->_urlbase.'/public/media/js/jquery.maskMoney.min.js');
        $this->view->headScript()->appendFile($this->_urlbase.'/public/media/js/jquery.maskedinput.min.js');
        $this->view->headScript()->appendFile($this->_urlbase.'/public/media/js/mask.js');
                
        $this->view->headScript()->appendFile($this->_urlbase.'/public/media/js/validation/dist/jquery.validate.min.js');
        $this->view->headScript()->appendFile($this->_urlbase.'/public/media/js/view/detalhe.js?1='.  rand(99, 99999));
        
        $id = $this->_getParam('id', 0);
        $this->view->id = $id;
        
        $query = 'SELECT SQL_CACHE * FROM GALERIA WHERE GAL_MODNUM = '.$id.' AND GAL_MOD like "procedimento" ORDER BY GAL_ORD ASC';
        $this->view->galeria = $this->db->fetchAll($query); 
        
        $query = 'SELECT SQL_CACHE PRC_ID,PRC_NOM FROM PROCEDIMENTO WHERE PRC_STA = 1 ORDER BY PRC_ORD ASC';
        $procs = $this->db->fetchAll($query);
        $this->view->procs = $procs;
        
        $query = 'SELECT SQL_CACHE * FROM PROCEDIMENTO WHERE PRC_ID = '.$id.' AND PRC_STA = 1';
        $row = $this->db->fetchRow($query);
        
        $this->view->row = $row;
        
        $prc_lclapl = (isset($row['PRC_LCLAPL'])) ? $row['PRC_LCLAPL']: '';
        $prc_ind    = (isset($row['PRC_IND']))    ? $row['PRC_IND']:    '';
        $prc_cntind = (isset($row['PRC_CNTIND'])) ? $row['PRC_CNTIND']: '';
        $prc_bnf    = (isset($row['PRC_BNF']))    ? $row['PRC_BNF']:    '';
        $prc_ses    = (isset($row['PRC_SES']))    ? $row['PRC_SES']:    '';
        $prc_prvrtd = (isset($row['PRC_PRVRTD'])) ? $row['PRC_PRVRTD']: '';
        $prc_itddor = (isset($row['PRC_DRCEFT'])) ? $row['PRC_DRCEFT']: '';
        $prc_dcreft = (isset($row['PRC_ITDDOR'])) ? $row['PRC_ITDDOR']: '';
        
        $result['item'][] = array('NOME' => 'PRC_LCLAPL', 'DESCRICAO' => $prc_lclapl, 'TITULO' => 'Local da aplica&ccedil;&atilde;o');
        $result['item'][] = array('NOME' => 'PRC_SES',    'DESCRICAO' => $prc_ses,    'TITULO' => 'Sess&otilde;es');
        $result['item'][] = array('NOME' => 'PRC_IND',    'DESCRICAO' => $prc_ind ,   'TITULO' => 'Indica&ccedil;&otilde;es');
        $result['item'][] = array('NOME' => 'PRC_PRVRTD', 'DESCRICAO' => $prc_prvrtd, 'TITULO' => 'Previs&atilde;o para resultado');
        $result['item'][] = array('NOME' => 'PRC_BNF',    'DESCRICAO' => $prc_bnf,    'TITULO' => 'Benef&iacute;cios');
        $result['item'][] = array('NOME' => 'PRC_ITDDOR', 'DESCRICAO' => $prc_dcreft, 'TITULO' => 'Intensidade da dor');
        //$result['item'][] = array('NOME' => 'PRC_DRCEFT', 'DESCRICAO' => $prc_itddor, 'TITULO' => 'Dura&ccedil;&atilde;o do efeito');
        //$result['item'][] = array('NOME' => 'PRC_CNTIND', 'DESCRICAO' => $prc_cntind, 'TITULO' => 'Contra indica&ccedil;&otilde;es');
        
        foreach ($result['item'] as $key => $row){
            if(strlen($row['DESCRICAO']) == 0){
                unset($result['item'][$key]);
            }
        }
        
       if($this->getRequest()->isPost()) {
            try {
                $formData = $this->getRequest()->getPost();
                //print_r($formData);
                
                $data = array(
                    'AGA_NOM'    => $formData['nome'],
                    'AGA_TEL'    => $formData['telefone'],
                    'AGA_EML'    => $formData['email'],
                    'AGA_CID'    => $formData['cidade'],
                    'AGA_QEX'    => $formData['queixa'],
                    'AGA_DAT'    => date('Y-m-d'),
                );
                
                $int = $this->db->insert('AGENDA_AVALIACAO', $data);
                
                Zend_Loader::loadClass('envioemail','library');
                $arr['queixa']  = $formData['queixa'];
                $arr['nome']    = $formData['nome'];
                $arr['email']   = $formData['email'];
                $arr['telefone']= $formData['telefone'];
                $arr['cidade']  = $formData['cidade'];
                $arr['urlbase'] = $this->_urlbase;
                $ok = envioemail::avaliacao($arr);
                
                echo "<script>alert('Agenda enviada com sucesso')</script>";
            } catch (Exception $exc) {
                //echo 'error: '.$exc->getMessage();
                echo "<script>alert('Não foi possível enviar a agenda')</script>";
            }     
        }
        
        $this->view->result = $result;
        
        
        $query = 'SELECT SQL_CACHE QEX_ID, QEX_NOM FROM QUEIXA WHERE QEX_STA = 1 ORDER BY QEX_NOM ASC';
        $queixa = $this->db->fetchAll($query);  
        $this->view->queixa = $queixa;
        
    }
    
    public function maisprocedimentosAction(){
        $this->_helper->layout->disableLayout(); 
        $this->_helper->viewRenderer->setNoRender(); 
        
        $offset = (int)$this->_getParam('offset', 0) + $this->limitRegistros;
        
        $query = 'SELECT SQL_CACHE PRC_ID,PRC_NOM FROM PROCEDIMENTO WHERE PRC_STA = 1 ORDER BY PRC_ORD ASC LIMIT '.$offset.','.$this->limitRegistros;
        $result = $this->db->fetchAll($query);
        
        $count = 0;
        $btn_mais = 1;
        foreach ($result as $res) $count += 1;
        if($count == 0){
            $offset = $offset - $this->limitRegistros;
            $btn_mais = 0;
        }
        else if($count < $this->limitRegistros) {
            $btn_mais = 0;
        }
        
        $html =  $this->view->partial('procedimentos/list-index.phtml', array('urlbase' => $this->_urlbase,'result'=>$result));
        
        echo json_encode(array('ok' => true, 'html' => $html,'offset'=>$offset,'btn_mais'=>$btn_mais));
        
    }
}
